package com.room.service;

import com.room.mapper.UserMapper;
import com.room.pojo.Role;
import com.room.pojo.Student;
import com.room.pojo.User;
import com.room.utils.UserUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @创建人 lin
 * @创建时间 2019/9/28
 * @描述
 */
@Service
@Transactional
public class UserService implements UserDetailsService {
     @Autowired
    UserMapper userMapper;


    //   通过用户名获取用户id
    public Long getUserIdByUserName(String username){
        return userMapper.getUserIdByUserName(username);
    }
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userMapper.loadUserByUsername(s);
        if(user == null){
            throw new UsernameNotFoundException("用户名有误");
        }
        return user;
    }


//往用户表插入数据
    public int userReg(String username, String password) {

        if (userMapper.loadUserByUsername(username) != null) {
            return -1;
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(password);
        return userMapper.userReg(username, encode);
    }

//    public List<User> getHrsByKeywords(String keywords) {
//        return userMapper.getUsersByKeywords(keywords);
//    }

        public List<User> getUsersByKeywords(String keywords) {
        return userMapper.getUsersByKeywords(keywords);
    }

    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

//    public int updateHrRoles(Long userId, Long[] rids) {
//        int i = userMapper.deleteRoleByUserId(userId);
//        return userMapper.addRolesForUser(userId, rids);
//    }
//
    //通过学工号添加角色，更新数据库
   public int addRolesForUser(Long userId,  Long[] rids){
        return userMapper.addRolesForUser(userId,rids);
    }
        public int updateUserRoles(Long userId, Long[] rids) {
        int i = userMapper.deleteRoleByUserId(userId);
        return userMapper.addRolesForUser(userId, rids);
    }
    public User getUserById(Long userId) {
        return userMapper.getUserById(userId);
    }

    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    public int deleteUser(Long userId) {
        return userMapper.deleteUser(userId);
    }

    public List<User> getAllUserExceptAdmin() {
        return userMapper.getAllUser(UserUtils.getCurrentUser().getId());
    }
    public List<User> getAllUser() {
        return userMapper.getAllUser(null);
    }




    public List<User> getUserByPage(Integer page, Integer size){
        int start  = (page-1)* size;
      return userMapper.getUserByPage(start, size);
  }

   public int updatepewd(String username,String encode){
        return userMapper.updatePwdByUsername(username,encode);
   }

   public List<Role> getRoleByUserId(Long userId){
        return userMapper.getRolesByUserId(userId);
   }

  //获取用户表的最大id
    public long getUserMaxId(){
        return userMapper.getUserMaxId();
    }

    //插入用户
    public int insertUser(User user){
        return userMapper.insetUser(user);
    }

  public int updateUserByUsername(User user){
      return   userMapper.updateUserByUsername(user);
  }
}
